﻿using System.Linq;

namespace Sidvall.IdentityManager.Data.IO.Model.StorageManagers
{
    public static partial class AspNetUsersManager
    {
        #region OnBeforeAddItem

        static partial void OnGetItems(string filePath, Sidvall.Business.DataPortalContext<IAspNetUsers, Sidvall.Data.FetchListCriteria<Fields.AspNetUsersField, Relations.AspNetUsersRelation>> dataPortalContext, Entities.AspNetUsers item, ref bool returnItem)
        {
            switch (dataPortalContext.OperationContext.CriteriaId)
            {
                case ParameterManager.FindUsersFromLoginCommandId:
                    returnItem = false;
                    if (item.AspNetUserLogins != null)
                    {
                        var loginProvider = dataPortalContext.Parameters.ValueToString(ParameterManager.LoginProviderParameterKey, null);
                        var providerKey = dataPortalContext.Parameters.ValueToString(ParameterManager.ProviderKeyParameterKey, null);
                        var login = (from o in item.AspNetUserLogins.Items
                                     where o.LoginProvider.Equals(loginProvider, System.StringComparison.OrdinalIgnoreCase) && o.ProviderKey.Equals(providerKey, System.StringComparison.OrdinalIgnoreCase)
                                     select o).FirstOrDefault();
                        returnItem = (login != null);
                    }
                    break;
                default:
                    returnItem = IsValid(dataPortalContext.OperationContext.Filter, item);
                    break;
            }
        }

        #endregion
        #region OnGetItem

        static partial void OnGetItem(string filePath, Sidvall.Business.DataPortalContext<IAspNetUsers, Sidvall.Data.FetchCriteria<Fields.AspNetUsersField, Relations.AspNetUsersRelation>> dataPortalContext, Entities.AspNetUsers item, ref bool returnItem)
        {
            returnItem = IsValid(dataPortalContext.OperationContext.Filter, item);
        }

        #endregion

        #region IsValid

        private static bool IsValid(global::Sidvall.Data.Filter<global::Sidvall.IdentityManager.Data.Fields.AspNetUsersField> filter, Entities.AspNetUsers item)
        {
            if (item == null)
                return false;
            var value = filter.FilterItems.GetFilterItemValue(Sidvall.IdentityManager.Data.Fields.AspNetUsersField.Email, null);
            if ((!string.IsNullOrWhiteSpace(value)) && ((string.IsNullOrWhiteSpace(item.Email)) || (!item.Email.Equals(value, System.StringComparison.OrdinalIgnoreCase))))
                return false;
            value = filter.FilterItems.GetFilterItemValue(Sidvall.IdentityManager.Data.Fields.AspNetUsersField.UserName, null);
            if ((!string.IsNullOrWhiteSpace(value)) && ((string.IsNullOrWhiteSpace(item.UserName)) || (!item.UserName.Equals(value, System.StringComparison.OrdinalIgnoreCase))))
                return false;
            value = filter.FilterItems.GetFilterItemValue(Sidvall.IdentityManager.Data.Fields.AspNetUsersField.Id, null);
            if ((!string.IsNullOrWhiteSpace(value)) && ((string.IsNullOrWhiteSpace(item.Id)) || (!item.Id.Equals(value, System.StringComparison.OrdinalIgnoreCase))))
                return false;
            return true;
        }

        #endregion
    }
}